package com.wisezone.net.imclient;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * socket客户端
 *
 */
public class IMClient {
	private static final int TYPE_TEXT = 1;
	private static final int TYPE_PICTURE = 2;
	private static final String HOST = "192.168.5.6";
	private static final int PORT = 9527;
	public Socket socket;

	/**
	 * 启动socket客户端
	 */
	public void startClient() {
		try {
			socket = new Socket(HOST, PORT);

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送图片 从文件输入流中读数据写入socket输出流
	 * 
	 * @param args
	 */
	public void sendPicture() {
		System.out.println("请输入发送图片地址 :  <如: d:/list/1.jpg>");
		String path = InputUtil.inputString();
		File file = new File(path);
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			OutputStream os = socket.getOutputStream();

			byte[] b = new byte[20];
			int len = 0;
			while ((len = in.read(b)) != -1) {
				os.write(b, 0, len);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e);
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 发送文本消息
	 */
	public void sendText() {
		try {
			PrintWriter pw = new PrintWriter(socket.getOutputStream());
			String message = readMessageByKeyBoard();
			pw.println(message);
			pw.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 1 ：文本
	 * 2： 图片
	 * @param type
	 */
	public void sendType(int type) {
		try {
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(type);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读键盘内容
	 * 
	 * @param args
	 */
	public String readMessageByKeyBoard() {
		String line = "";
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			System.out.println("请输入发送内容: ");
			line = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;
	}

	/**
	 * 发送图片或文本
	 */
	public void sendMessage() {
		System.out.println("          欢迎进入即时通讯系统");
		while (true) {
			System.out.println("<输入操作命令: 1 发送文本消息, 2 发送图片, 3 退出系统>");
			int common = InputUtil.inputInt();
			switch (common) {
			case 1:
				sendType(TYPE_TEXT);
				sendText();
				System.out.println("发送文本成功");
				break;
			case 2:
				sendType(TYPE_PICTURE);
				sendPicture();
				System.out.println("发送图片成功!");
				break;
			}
		}
	}

	public static void main(String[] args) {
		IMClient client = new IMClient();
		client.startClient();
//		client.sendText();
		client.sendMessage();
	}
}
